/**
 * 
 */
package com.jxc.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jxc.dao.BaseDaoI;
import com.jxc.model.po.Goods;
import com.jxc.model.po.Salelist;
import com.jxc.model.po.SalelistItem;
import com.jxc.model.vo.DataGrid;
import com.jxc.model.vo.VSalelistItem;
import com.jxc.service.SalelistItemServiceI;

/**
 * @author Tino
 * 
 */
@Service("salelistItemService")
public class SalelistItemServiceImpl implements SalelistItemServiceI {

	@Autowired
	private BaseDaoI<SalelistItem> salelistItemDao;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.jxc.service.SalelistItemServiceI#batchSave(java.util.List,
	 *      java.lang.Long)
	 */
	@Override
	public void batchSave(List<VSalelistItem> vslis, Long slid) {
		for (VSalelistItem vsli : vslis) {
			vsli.setSlid(slid);
			this.save(vsli);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.jxc.service.SalelistItemServiceI#combobox(com.jxc.model.vo.VSalelistItem)
	 */
	@Override
	public List<VSalelistItem> combobox(VSalelistItem vsli) {
		return this.changeModel(this.find(vsli));
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.jxc.service.SalelistItemServiceI#datagrid(com.jxc.model.vo.VSalelistItem)
	 */
	@Override
	public DataGrid datagrid(VSalelistItem vsli) {
		DataGrid dg = new DataGrid();
		dg.setRows(this.changeModel(this.find(vsli)));
		dg.setTotal(this.total(vsli));
		return dg;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.jxc.service.SalelistItemServiceI#delete(java.lang.String)
	 */
	@Override
	public void delete(String ids) {
		if (ids != null) {
			for (String id : ids.split(",")) {
				if (!id.trim().equals("0")) {
					SalelistItem ct = this.salelistItemDao.get(SalelistItem.class, Long.parseLong(id.trim()));
					if (ct != null) {
						this.salelistItemDao.delete(ct);
					}
				}
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.jxc.service.SalelistItemServiceI#save(com.jxc.model.vo.VSalelistItem)
	 */
	@Override
	public void save(VSalelistItem vsli) {
		SalelistItem sli = new SalelistItem();
		BeanUtils.copyProperties(vsli, sli);
		if (vsli.getGid() != null) {
			sli.setGoods(new Goods(vsli.getGid()));
		}
		if (vsli.getSlid() != null) {
			sli.setSalelist(new Salelist(vsli.getSlid()));
		}
		this.salelistItemDao.save(sli);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.jxc.service.SalelistItemServiceI#update(com.jxc.model.vo.VSalelistItem)
	 */
	@Override
	public void update(VSalelistItem vsli) {
		SalelistItem sli = this.salelistItemDao.get(SalelistItem.class, vsli.getId());
		BeanUtils.copyProperties(vsli, sli, new String[] { "id" });
		if (vsli.getGid() != null) {
			sli.setGoods(new Goods(vsli.getId()));
		}
		if (vsli.getSlid() != null) {
			sli.setSalelist(new Salelist(vsli.getSlid()));
		}
		this.salelistItemDao.update(sli);
	}

	private List<VSalelistItem> changeModel(List<SalelistItem> slis) {
		List<VSalelistItem> vslis = new ArrayList<VSalelistItem>();
		if (slis != null && slis.size() > 0) {
			for (SalelistItem sli : slis) {
				VSalelistItem vsli = new VSalelistItem();
				BeanUtils.copyProperties(sli, vsli);
				if (sli.getGoods() != null) {
					Goods goods = sli.getGoods();
					vsli.setGid(goods.getId());
					vsli.setGname(goods.getName());
				}
				if (sli.getSalelist() != null) {
					Salelist sl = sli.getSalelist();
					vsli.setSlid(sl.getId());
				}
				vslis.add(vsli);
			}
		}
		return vslis;
	}

	private List<SalelistItem> find(VSalelistItem vsli) {
		String hql = "from SalelistItem t where 1=1 ";

		List<Object> values = new ArrayList<Object>();
		hql = addWhere(vsli, hql, values);

		if (vsli.getSort() != null && vsli.getOrder() != null) {
			hql += " order by " + vsli.getSort() + " " + vsli.getOrder();
		}
		return this.salelistItemDao.find(hql, values, vsli.getPage(), vsli.getRows());
	}

	private Long total(VSalelistItem vsli) {
		String hql = "select count(*) from SalelistItem t where 1=1 ";
		List<Object> values = new ArrayList<Object>();
		hql = addWhere(vsli, hql, values);
		return this.salelistItemDao.count(hql, values);
	}

	private String addWhere(VSalelistItem vsli, String hql, List<Object> values) {
		if (vsli.getId() != null) {
			hql += " and t.id = ? ";
			values.add(vsli.getId());
		}

		if (vsli.getGid() != null) {
			hql += " and t.goods.id = ? ";
			values.add(vsli.getGid());
		}

		if (vsli.getSlid() != null) {
			hql += " and t.salelist.id = ? ";
			values.add(vsli.getSlid());
		}
		return hql;
	}
}
